2025-04-01
class ClassName:
def __init__(self, parameters):
# Constructor
self.attribute = parameters
def method(self, parameters):
# Method
return self.attribute + parametersClassName is the name of the class__init__ is the constructorself is the instance of the classparameters are the function inputsself.attribute is the attribute of the classmethod is a method of the class
return is the output of the methodselfself is a reference to the current instance of the classself with dot notation allows you access/modification of the class
self.attributeself is passed as first argument to the methodself could be also called this, cls, or any other nameself in Pythontest_experiment = Experiment("LTP", "2025-04-15")
type(test_experiment)
test_experiment.name
test_experiment.date
test_experiment.add_data([1.41, 1.38, 1.39])
test_experiment.get_data()__main__.Experiment
'LTP'
'2025-04-15'
[[1.41, 1.38, 1.39]]
test_experiment is an instance of the class Experimentname and date are attributes of the class (dot notation)add_data and get_data are methods of the class (dot notation)def average_LTP(experiment: Experiment) -> float:
"""
Calculate the average of the data in an Experiment object.
Parameters:
experiment (Experiment): The Experiment object containing data.
Returns:
float: The average of the data.
"""
import numpy as np
if not isinstance(experiment, Experiment):
raise TypeError("Input must be an Experiment object.")
return np.mean(experiment.data)Experiment class as inputExperiment objectnp.random.seed(0)
trace = ripple + gaussian*6
# repeat the trace 4 times
# add noise
trace = np.tile(trace, 4)
trace += np.random.normal(0, 3, size=trace.shape)
# add time to the trace
time = np.arange(start=0, step=time[1], stop=len(trace)*time[1])
plt.plot(time, trace)
plt.show()trace and time to create an event classEvent class